.\" .\" UCSD p-System cross compiler .\" Copyright (C) 2011, 2012 Peter Miller .\" .\" This program is free software; you can redistribute it and/or modify .\" it under the terms of the GNU General Public License as published by .\" the Free Software Foundation; either version 2 of the License, or (at .\" you option) any later version. .\" .\" This program is distributed in the hope that it will be useful, .\" but WITHOUT ANY WARRANTY; without even the implied warranty of .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU .\" General Public License for more details. .\" .\" You should have received a copy of the GNU General Public License along .\" with this program. If not, see .\" .nf .ad l .hy 0 Miller, P. A. (2010) Factory Factory Factories
Up, Prev Next

Revision: Imperative vs OO

\&... imperative type-based dispatch \&... facility of the C++ language
void expr_generate_code(struct expr *e);

void
expr_generate_code(expr *e)
{
    switch(e->e_op)
    {
class expression
{
public:
    virtual void generate_code() const = 0;
};
    case PLUS:
        expr_generate_code(e->e_lhs);
        expr_generate_code(e->e_rhs);
        emit(OP_ADI);
        break;
class expression_add:
    public expression
{
public:
    virtual void generate_code() const {
        lhs->generate_code();
        rhs->generate_code();
        gen_emit_byte(OP_ADI);
    }

private:
    expression *lhs;
    expression *rhs;
}
    case INT_CONST:
        gen_emit_byte(OP_SLDC);
        gen_emit_int(e->e_int);
        break;
class expression_int_const:
    public expression
{
public:
    virtual void generate_code() const {
        gen_emit_byte(OP_SLDC);
        gen_emit_int(e->value);
        break;
    }

private:
    short value;
}
    etc etc
    }
}
etc etc
.\" vim: set ts=8 sw=4 et :